import db
import KBEDebug as log
import KBEngine
import time
import math
import random

class TaskInfoTable:
    def __init__(self, table_name):
        self.table_name = table_name
        self.tb = db.DBTable(table_name)
        # 任务ID
        self.tb.setFiled('taskId', db.INT32())
        # 任务名称
        self.tb.setFiled('name', db.UNICODE(128))
        # 奖励金币数
        self.tb.setFiled('gold', db.INT32())
        # 跳转链接
        self.tb.setFiled('link', db.UNICODE(128))
        # 最大达成数
        self.tb.setFiled('maxAchieveCount', db.INT32())
        # 是否启用
        self.tb.setFiled('isEnable', db.INT32())
        # 在TaskUI中的排序
        self.tb.setFiled('sort', db.UINT32())
        # 完成人数
        self.tb.setFiled('count', db.INT32())

    def createTable(self, success_callback):
        self.tb.exec(success_callback)

class ILobbyTaskInfo:
    def __init__(self):
        self.tb_taskInfo = TaskInfoTable('lobby_task_info')
        self.tb_create_cnt = 1
        self.tb_taskInfo.createTable(self.createTableSuccess)
        self.task_info = {}
        self.task_info_lst = []

    def createTableSuccess(self):
        self.tb_create_cnt -= 1
        if self.tb_create_cnt <= 0:
            pass

    def addTaskInfoCount(self, task_id):
        def callback(record):
            if len(record) == 0:
                log.DEBUG_MSG(f'没有找到对应任务: {task_id}')
            else:
                # 记录已存在，增加完成
                sql = f'UPDATE lobby_task_info SET count = count + 1 WHERE taskId = \"{task_id}\"'
                log.DEBUG_MSG(f'更新任务信息记录: {task_id}')
                KBEngine.executeRawDatabaseCommand(sql, None)
        
        self.tb_taskInfo.tb.loadAll(f'taskId=\"{task_id}\"', callback)
        
    def reqTaskInfo(self, entity_call):
        self.task_info.clear()
        self.task_info_lst.clear()
        def callback(result, rows, insertid, error):
            if error:
                log.ERROR_MSG(f'loadAllTaskInfo failed: {error}')
            else:
                log.DEBUG_MSG(f'loadAllTaskInfo success {len(result)}')
                result_lst = []
                for line in result:
                    task_taskId = int(line[0])
                    task_name = str(line[1], 'utf-8')
                    task_gold = int(line[2])
                    task_link = str(line[3])
                    task_maxAchieveCount = int(line[4])
                    task_isEnable = int(line[5]) 
                    task_sort = int(line[6])
                    task_count = int(line[7])
                    item = {
                        'taskId': task_taskId,
                        'name': task_name,
                        'gold': task_gold,
                        'link': task_link,
                        'maxAchieveCount': task_maxAchieveCount,
                        'isEnable': task_isEnable,
                        'sort': task_sort,
                        'count': task_count
                    }
                    self.task_info[task_taskId] = item
                    result_lst.append(item)
                self.task_info_lst = result_lst
                if  entity_call is not None:
                    total_count = len(self.task_info_lst)
                    if total_count == 0:
                        entity_call.client.repNoTaskInfo()
                    else:
                        entity_call.client.repTaskInfo(self.task_info_lst, 0, total_count)
                    log.DEBUG_MSG('获取TaskInfo结束')

        sql = 'select taskId, name, gold, link, maxAchieveCount, isEnable, sort, count from lobby_task_info ORDER BY taskId ASC;'
        KBEngine.executeRawDatabaseCommand(sql, callback)
    